package com.collect.tanzania.zhiScene.zhiOrderReview.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiSend.ZHISendEntity;
import com.collect.tanzania.zhiScene.zhiOrderReview.zhiFollow.adapter.ZHIOrderReviewContactColumn;
import com.collect.tanzania.zhiScene.zhiOrderReview.zhiFollow.bean.ZHIROrderReviewDetailEntity;
import com.collect.tanzania.zhiScene.zhiOrderReview.zhiFollow.bean.ZHISOrderReviewDetailEntity;
import com.collect.tanzania.zhiScene.zhiWidget.zhiDialog.ZHIDialogAvatar;
import com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;
import com.collect.tanzania.zhiUtils.StringUtil;
import com.collect.tanzania.zhiUtils.ToastUtil;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIOrderReviewFollowFragment extends BaseFragment<ZHIOrderReviewFollowPresenter> implements ZHIOrderReviewFollowContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.textApplyTime)
    TextView textApplyTime;

    @BindView(R.id.textBvnNum)
    TextView textBvnNum;
    @BindView(R.id.textFullName)
    TextView textFullName;

    @BindView(R.id.textGender)
    TextView textGender;
    @BindView(R.id.textDob)
    TextView textDob;
    @BindView(R.id.textPhone)
    TextView textPhone;
    @BindView(R.id.textMaritalStatus)
    TextView textMaritalStatus;
    @BindView(R.id.textHighestEducation)
    TextView textHighestEducation;
    @BindView(R.id.textLanguage)
    TextView textLanguage;
    @BindView(R.id.textReligion)
    TextView textReligion;
    @BindView(R.id.textEmploymentType)
    TextView textEmploymentType;
    @BindView(R.id.textMonthlyIncome)
    TextView textMonthlyIncome;
    @BindView(R.id.textWorkInfoTime)
    TextView textWorkInfoTime;

    @BindView(R.id.rc_contact_list)
    RecyclerView rc_contact_list;

    @BindView(R.id.imageProofFace)
    ImageView imageProofFace;
    @BindView(R.id.textProofFaceNoPic)
    TextView textProofFaceNoPic;

    @BindView(R.id.imageProofFront)
    ImageView imageProofFront;
    @BindView(R.id.textProofFrontNoPic)
    TextView textProofFrontNoPic;

    @BindView(R.id.imageProofBack)
    ImageView imageProofBack;
    @BindView(R.id.textProofBackNoPic)
    TextView textProofBackNoPic;

    @BindView(R.id.cbResultPass)
    CheckBox cbResultPass;
    @BindView(R.id.cbResultReject)
    CheckBox cbResultReject;

    @BindView(R.id.cbReject1)
    CheckBox cbReject1;
    @BindView(R.id.cbReject2)
    CheckBox cbReject2;
    @BindView(R.id.cbReject3)
    CheckBox cbReject3;
    @BindView(R.id.cbReject4)
    CheckBox cbReject4;


    @BindView(R.id.inputRejectRemark)
    EditText inputRejectRemark;

    @BindView(R.id.btnResultSubmit)
    TextView btnResultSubmit;

    private String mProofFaceUrl = "";
    private String mProofFrontUrl = "";
    private String mProofBackUrl = "";


    private final ZHIOrderReviewContactColumn mZHIOrderReviewContactColumn = new ZHIOrderReviewContactColumn();

//    private final ZHIOrderReviewFollowAdjuster mZHIOrderReviewFollowAdjuster = new ZHIOrderReviewFollowAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_review_follow;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            rc_contact_list.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            rc_contact_list.setAdapter(mZHIOrderReviewContactColumn);

            navigationBar.hideLeft().hideRight().setCenterText(getString(R.string.order_review_page_title));

            cbResultPass.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (buttonView.equals(cbResultPass)) {
                    if (isChecked) {
                        cbResultReject.setChecked(false);
                    }
                }
            });

            cbResultReject.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (buttonView.equals(cbResultReject)) {
                    if (isChecked) {
                        cbResultPass.setChecked(false);
                    }
                }
            });

            btnResultSubmit.setOnClickListener(v -> {
                if (cbResultReject.isChecked()) {
                    String input = StringUtil.safeString(inputRejectRemark.getText().toString().trim());
                    String options = "";

                    if (cbReject1.isChecked()) {
                        options += "用户信息造假";
                    }
                    if (cbReject2.isChecked()) {
                        if (!TextUtils.isEmpty(options)) {
                            options += ",";
                        }
                        options += "联系方式造假";
                    }
                    if (cbReject3.isChecked()) {
                        if (!TextUtils.isEmpty(options)) {
                            options += ",";
                        }
                        options += "上传的照片不是护照照片";
                    }
                    if (cbReject4.isChecked()) {
                        if (!TextUtils.isEmpty(options)) {
                            options += ",";
                        }
                        options += "上传的不是人脸";
                    }

                    if (TextUtils.isEmpty(options)) {
                        ToastUtil.s(getString(R.string.order_review_result_subbmit_err1));
                        return;
                    }

//                    if (TextUtils.isEmpty(input)) {
//                        ToastUtil.s(getString(R.string.order_review_result_subbmit_err2));
//                        return;
//                    }

                    if (mPresenter != null) {
                        ZHISOrderReviewDetailEntity zhisOrderReviewDetailEntity = new ZHISOrderReviewDetailEntity();
                        zhisOrderReviewDetailEntity.setMobile(textPhone.getText().toString().trim());
                        zhisOrderReviewDetailEntity.setOrderCheckStatus("REJECT");
                        zhisOrderReviewDetailEntity.setHandleInfo(input);
                        zhisOrderReviewDetailEntity.setRejectInfo(options);
                        mPresenter.handleOrderReview(requireActivity(), zhisOrderReviewDetailEntity);
                    }
                } else {
                    if (mPresenter != null) {
                        String input = StringUtil.safeString(inputRejectRemark.getText().toString().trim());
                        ZHISOrderReviewDetailEntity zhisOrderReviewDetailEntity = new ZHISOrderReviewDetailEntity();
                        zhisOrderReviewDetailEntity.setMobile(textPhone.getText().toString().trim());
                        zhisOrderReviewDetailEntity.setOrderCheckStatus("PASS");
                        zhisOrderReviewDetailEntity.setHandleInfo(input);
                        mPresenter.handleOrderReview(requireActivity(), zhisOrderReviewDetailEntity);
                    }
                }
            });

            imageProofFace.setOnClickListener(v -> {
                try {
                    if (!TextUtils.isEmpty(mProofFaceUrl) && mProofFaceUrl.startsWith("http")) {
                        ZHIDialogAvatar.showDialog(requireActivity(), mProofFaceUrl);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            imageProofFront.setOnClickListener(v -> {
                try {
                    if (!TextUtils.isEmpty(mProofFrontUrl) && mProofFrontUrl.startsWith("http")) {
                        ZHIDialogAvatar.showDialog(requireActivity(), mProofFrontUrl);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            imageProofBack.setOnClickListener(v -> {
                try {
                    if (!TextUtils.isEmpty(mProofBackUrl) && mProofBackUrl.startsWith("http")) {
                        ZHIDialogAvatar.showDialog(requireActivity(), mProofBackUrl);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData(true);
    }

    @Override
    public void onGetErrorInfo() {
//        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onShowOrderReviewDetail(ZHIROrderReviewDetailEntity zhirOrderReviewDetailEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            textApplyTime.setText(zhirOrderReviewDetailEntity.getApplyTime());

            textBvnNum.setText(zhirOrderReviewDetailEntity.getUser().getBvnNumber());
            textFullName.setText(zhirOrderReviewDetailEntity.getUser().getFullName());
            textGender.setText(zhirOrderReviewDetailEntity.getUser().getGender());
            textDob.setText(zhirOrderReviewDetailEntity.getUser().getBirthDate());
            textPhone.setText(zhirOrderReviewDetailEntity.getUser().getMobile());
            textMaritalStatus.setText(zhirOrderReviewDetailEntity.getUser().getMaritalStatus());
            textHighestEducation.setText(zhirOrderReviewDetailEntity.getUser().getHighestEducation());
            textLanguage.setText(zhirOrderReviewDetailEntity.getUser().getLanguage());
            textReligion.setText(zhirOrderReviewDetailEntity.getUser().getReligion());
            textEmploymentType.setText(zhirOrderReviewDetailEntity.getUser().getEmploymentType());
            textMonthlyIncome.setText(zhirOrderReviewDetailEntity.getUser().getMonthlyIncome());
            textWorkInfoTime.setText(zhirOrderReviewDetailEntity.getUser().getWorkInfoTime());

            mZHIOrderReviewContactColumn.setNewData(zhirOrderReviewDetailEntity.getUserContacts());

            String faceImg = zhirOrderReviewDetailEntity.getUser().getFaceImg();
            mProofFaceUrl = faceImg;
            if (!TextUtils.isEmpty(faceImg)) {
                imageProofFace.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(this).load(faceImg).into(imageProofFace);
                textProofFaceNoPic.setVisibility(View.GONE);
            } else {
                imageProofFace.setImageResource(R.drawable.ic_image_place);
                imageProofFace.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                textProofFaceNoPic.setVisibility(View.VISIBLE);
            }

            String frontImg = zhirOrderReviewDetailEntity.getUser().getProofFrontImg();
            mProofFrontUrl = frontImg;
            if (!TextUtils.isEmpty(frontImg)) {
                imageProofFront.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(this).load(frontImg).into(imageProofFront);
                textProofFrontNoPic.setVisibility(View.GONE);
            } else {
                imageProofFront.setImageResource(R.drawable.ic_image_place);
                imageProofFront.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                textProofFrontNoPic.setVisibility(View.VISIBLE);
            }

            String backImg = zhirOrderReviewDetailEntity.getUser().getProofFrontImg();
            mProofBackUrl = backImg;
            if (!TextUtils.isEmpty(backImg)) {
                imageProofBack.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(this).load(backImg).into(imageProofBack);
                textProofBackNoPic.setVisibility(View.GONE);
            } else {
                imageProofBack.setImageResource(R.drawable.ic_image_place);
                imageProofBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                textProofBackNoPic.setVisibility(View.VISIBLE);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onHandleOrderReview() {
        try {
            srl_swipe_layout.setRefreshing(false);
            resetMarkUI();
            initData(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            srl_swipe_layout.setRefreshing(false);
            mPresenter.showOrderReviewDetail(getActivity(), new ZHISendEntity(), showLoading);
        }
    }

    private void resetMarkUI() {
        try {
            cbResultPass.setChecked(false);
            cbResultReject.setChecked(false);

            cbReject1.setChecked(false);
            cbReject2.setChecked(false);
            cbReject3.setChecked(false);
            cbReject4.setChecked(false);

            inputRejectRemark.setText(null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshData() {
        initData(true);
    }

    private boolean checkCanCommit() {
//        try {
//            mStatus = "";
//            String remark = StringUtil.safeString(et_remark.getText().toString().trim());
//
//            if (TextUtils.isEmpty(mOrderId)) {
//                ToastUtil.s("User not found, Please check again.");
//                return false;
//            }
//
//            if (cb_reject.isChecked()) {
//                if (TextUtils.isEmpty(remark)) {
//                    ToastUtil.s("Please enter follow content.");
//                    return false;
//                }
//                mStatus = ZHISPhoneReviewMarkedImpl.DS_DENIED;
//                return true;
//            }
//
//            if (cb_mark.isChecked()) {
//                if (TextUtils.isEmpty(remark)) {
//                    ToastUtil.s("Please enter follow content.");
//                    return false;
//                }
//                mStatus = ZHISPhoneReviewMarkedImpl.DS_SUSPEND;
//                return true;
//            }
//
//            if (cb_pass.isChecked()) {
//                mStatus = ZHISPhoneReviewMarkedImpl.DS_APROVAL;
//                return true;
//            }
//
//            ToastUtil.s("Please select the follow status.");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
        return false;
    }

}
